<?php
class Wxapplet
{
	public function get_access_token(){
		
		$this->CI =& get_instance();
		$this->CI->load->driver('cache');
		
		$access_token = $this->CI->cache->memcached->get("ACCESSTOKEN".APPID);
		
		if(empty($access_token)){
			$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.APPID.'&secret='.APPSEC;
			$token_data = json_decode($this->http_get($url));
			
			$this->CI->cache->memcached->save("ACCESSTOKEN".APPID,$token_data->access_token,$token_data->expires_in);
			$access_token = $token_data->access_token;
		}
		//echo $token_data->expires_time ." < ". time()."<br/>";
		return $access_token;
	}

	function http_get($url){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
		}
		curl_setopt($oCurl, CURLOPT_URL, $url);
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}

	//json only
	function http_post($url,$data){
		$oCurl = curl_init();
		if(stripos($url,"https://")!==FALSE){
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
		}
		//$header = array("Content-Type: application/json; encoding=utf-8");
		curl_setopt($oCurl, CURLOPT_URL, $url);//设置链接
		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
		curl_setopt($oCurl, CURLOPT_POST, 1);//设置为POST方式
		curl_setopt($oCurl, CURLOPT_POSTFIELDS, $data);//POST数据
		$sContent = curl_exec($oCurl);
		$aStatus = curl_getinfo($oCurl);
		curl_close($oCurl);
		if(intval($aStatus["http_code"])==200){
			return $sContent;
		}else{
			return false;
		}
	}
	
	//https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#发送模板消息
	// {
	  // "touser": "OPENID",  
	  // "template_id": "TEMPLATE_ID", 
	  // "page": "index",          
	  // "form_id": "FORMID",         
	  // "data": {
		  // "keyword1": {
			  // "value": "339208499", 
			  // "color": "#173177"
		  // }, 
		  // "keyword2": {
			  // "value": "2015年01月05日 12:30", 
			  // "color": "#173177"
		  // }, 
		  // "keyword3": {
			  // "value": "粤海喜来登酒店", 
			  // "color": "#173177"
		  // } , 
		  // "keyword4": {
			  // "value": "广州市天河区天河路208号", 
			  // "color": "#173177"
		  // } 
	  // },
	  // "emphasis_keyword": "keyword1.DATA" 
	// }
	public function send_template_message($touser,$template_id,$page,$form_id,$data)
	{
		foreach($data as $key => $value)
		{
			$data[$key]["value"] = urlencode($data[$key]["value"]);
		}
		
		$message_data = array(
			"touser" => $touser,
			"template_id" => $template_id,
			"page" => $page,
			"form_id" => $form_id,
			"data" => $data,
//			"emphasis_keyword"=>$emphasis_keyword
		);
		
		$final_str = urldecode(json_encode($message_data));
		//echo $final_str;
		$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$this->get_access_token();
		$ret = $this->http_post($url, $final_str);
        $content = json_decode($ret, true);
        return $content;
	}
}
